package com.gm.wholesale.module.main.presenter

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.gm.commonlib.util.logUtils.LogUtil
import com.gm.wholesale.R
import com.gm.wholesale.constant.MenuData
import com.gm.wholesale.module.aftersale.ui.AftersaleFragment
import com.gm.wholesale.module.custom.bean.CustomBean
import com.gm.wholesale.module.custom.ui.customManger.CreateCustomFragment
import com.gm.wholesale.module.custom.ui.customManger.CustomListFragment
import com.gm.wholesale.module.evenbus.EvenBusUtil
import com.gm.wholesale.module.good.bean.GoodListBean
import com.gm.wholesale.module.good.ui.goodManger.CreateGoodFragment
import com.gm.wholesale.module.good.ui.goodManger.GoodMangerFragment
import com.gm.wholesale.module.good.ui.mainGood.GoodMainFragment
import com.gm.wholesale.module.main.ui.MainActivity
import com.gm.wholesale.module.order.bean.OrderDetailBean
import com.gm.wholesale.module.order.ui.MainOrderFragment
import com.gm.wholesale.module.order.ui.OrderMangerFragment
import com.gm.wholesale.module.report.ui.ReportMangerFragment
import com.gm.wholesale.module.setting.ui.SettingFragment
import com.gm.wholesale.module.settlement.ui.main.AccountMainFragment
import com.gm.wholesale.module.webview.mainWebView.MainWebFragment
import com.gm.wholesale.view.HeaderView

class MainPresenter(private var mActivity: MainActivity,
                    private var mMainOrderFragment: MainOrderFragment) {
    private  var mManager: FragmentManager? = null;
    private  var mTransaction: FragmentTransaction? = null

    // todo 主界面右边商品部分
    private  var mGoodMainFragment: GoodMainFragment? = null

    //todo 订单管理
    private  var orderMangerFragment: OrderMangerFragment? = null

    //todo 设置
    private  var settingFragment: SettingFragment? = null

    //todo 对账
    private var accountMainFragment: AccountMainFragment? = null

    //todo 网页相关，客户，应收，报表，商品
    private var mainWebFragment: MainWebFragment? = null

    //todo 售后
    private var aftersaleFragment: AftersaleFragment? = null

    //todo 商品管理
    private var goodMangerFragment: GoodMangerFragment? = null

    //创建商品
    private var mCreateGoodFragment: CreateGoodFragment? = null
    private var mCustomListFragment: CustomListFragment? = null //客户列表
    private var createCustomFragment: CreateCustomFragment? = null
    private var mReportMangerFragment: ReportMangerFragment? = null //报表模块
    private val fragments: MutableList<Fragment> = ArrayList()

    init {
        initMainView()
    }

    private fun initMainView() {
        mManager=mActivity.supportFragmentManager;
        mGoodMainFragment = mManager?.findFragmentById(R.id.goodFragment) as GoodMainFragment?

    }

    private fun hidMainUI() {
        if (mGoodMainFragment != null) {
            hidUI(mGoodMainFragment)
        }
        if (mMainOrderFragment != null) {
            hidUI(mMainOrderFragment)
        }
    }

    //todo 显示首页主界面
    fun showMainUI() {
        if (mGoodMainFragment != null) {
            showUI(mGoodMainFragment)
        }
        if (mMainOrderFragment != null) {
            showUI(mMainOrderFragment)
        }
        for (fragment in fragments) {
            removeFragment(fragment)
        }
        fragments.clear()
        //关闭对账单详情界面 事件
        EvenBusUtil.closeSettleMentOrderDetail()
    }

    fun showFunctionUI(mCurrentMenuData: MenuData, headerView: HeaderView?) {
        hidMainUI()
        //订单管理界面
        if (mCurrentMenuData.type == MenuData.ORDER_MANGER.type) {
            showOrderMangerUI()
        } else if (mCurrentMenuData.type == MenuData.GOOD_MANGER.type) {
            if (goodMangerFragment == null) {
                goodMangerFragment = GoodMangerFragment()
            }
            showReplaceUI(goodMangerFragment)
        } else if (mCurrentMenuData.type == MenuData.SETTING.type) {
            if (settingFragment == null) {
                settingFragment = SettingFragment()
            }
            showReplaceUI(settingFragment)
        } else if (mCurrentMenuData.type == MenuData.CUSTOM_MANGER.type) {
            if (mCustomListFragment == null) {
                mCustomListFragment = CustomListFragment()
                mCustomListFragment!!.setIsGetData(true)
            }
            showReplaceUI(mCustomListFragment)
        } else if (mCurrentMenuData.type == MenuData.RECEIV_MONEY.type) {
            if (accountMainFragment == null) {
                accountMainFragment = AccountMainFragment()
            }
            showReplaceUI(accountMainFragment)
        } else if (mCurrentMenuData.type == MenuData.SALES_REPORT.type) {
            if (mReportMangerFragment == null) {
                mReportMangerFragment = ReportMangerFragment()
            }
            showReplaceUI(mReportMangerFragment)
        } else {
            if (mainWebFragment != null) {
                mainWebFragment!!.onDestroy()
                mainWebFragment = null
            }
            mainWebFragment = MainWebFragment()
            mainWebFragment!!.setHeaderView(headerView)
            val bundle = Bundle()
            bundle.putString(MainWebFragment.key, mCurrentMenuData.path)
            mainWebFragment!!.arguments = bundle
            showReplaceUI(mainWebFragment)
        }
    }

    fun showOrderMangerUI() {
        if (orderMangerFragment == null) {
            orderMangerFragment = OrderMangerFragment()
        }
        showReplaceUI(orderMangerFragment)
    }

    //todo 显示售后界面
    fun showAftersaleFragment(data: OrderDetailBean?) {
        aftersaleFragment = AftersaleFragment()
        fragments.add(aftersaleFragment!!)
        val bundle = Bundle()
        bundle.putSerializable("data", data)
        aftersaleFragment!!.arguments = bundle
        mTransaction = mManager!!.beginTransaction()
        mTransaction!!.add(R.id.mainView, aftersaleFragment!!)
        mTransaction!!.commitAllowingStateLoss()
    }

    fun showCreateGoodUI(goodBeans: GoodListBean?) {
        mCreateGoodFragment = CreateGoodFragment()
        fragments.add(mCreateGoodFragment!!)
        if (goodBeans != null) {
            val bundle = Bundle()
            bundle.putSerializable("data", goodBeans)
            mCreateGoodFragment!!.arguments = bundle
        }
        mTransaction = mManager!!.beginTransaction()
        mTransaction!!.add(R.id.mainView, mCreateGoodFragment!!)
        mTransaction!!.commitAllowingStateLoss()
    }

    fun showCreateCustormUI(customBean: CustomBean?) {
        createCustomFragment = CreateCustomFragment()
        fragments.add(createCustomFragment!!)
        if (customBean != null) {
            val bundle = Bundle()
            bundle.putSerializable("data", customBean)
            createCustomFragment!!.arguments = bundle
        }
        mTransaction = mManager!!.beginTransaction()
        mTransaction!!.add(R.id.mainView, createCustomFragment!!)
        mTransaction!!.commitAllowingStateLoss()
    }

    //改方法会销毁前面加载的fragment
    private fun showReplaceUI(fragment: Fragment?) {
        if (mManager == null) return
        mTransaction = mManager!!.beginTransaction()
        if (mTransaction == null) return
        if (fragment == null) return
        fragments.add(fragment)
        mTransaction!!.replace(R.id.mainView, fragment)
        mTransaction!!.commitAllowingStateLoss()
    }


    private fun hidUI(fragment: Fragment?) {
        if (mManager == null) return
        mTransaction = mManager!!.beginTransaction()
        if (mTransaction == null) return
        if (fragment == null) return
        mTransaction!!.hide(fragment)
        mTransaction!!.commitAllowingStateLoss()
    }

    private fun showUI(fragment: Fragment?) {
        if (mManager == null) return
        mTransaction = mManager!!.beginTransaction()
        if (mTransaction == null) return
        if (fragment == null) return
        mTransaction!!.show(fragment)
        mTransaction!!.commitAllowingStateLoss()
    }

    private fun removeFragment(fragment: Fragment?) {
        if (mManager == null) return
        mTransaction = mManager!!.beginTransaction()
        if (mTransaction == null) return
        if (fragment == null) return
        mTransaction!!.remove(fragment)
        mTransaction!!.commitAllowingStateLoss()
    }
}